package ro.ubbcluj.cs.damate.persistence.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.sql.Date;
import java.util.List;

import static javax.persistence.CascadeType.*;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.*;

/**
 * @author Lóránd Fazakas (lorand)
 *         Date: 4/22/12
 *         Time: 1:13 AM
 */
@Table(name = "team", catalog = "damate")
@Entity
public class Team {

    private Long teamId;

    @Column(name = "teamId", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    @GeneratedValue(strategy= IDENTITY)
    public Long getTeamId() {
        return teamId;
    }

    public void setTeamId(Long teamId) {
        this.teamId = teamId;
    }

    private String teamName;

    @Column(name = "teamName", nullable = false, insertable = true, updatable = true, length = 45, precision = 0)
    @Basic
    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }

    private Date creationDate;

    @Column(name = "creationDate", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Basic
    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    private User teamLeader;

    @ManyToOne(fetch = LAZY, cascade = REFRESH)
    @JoinColumn(name = "teamLeader")
    public User getTeamLeader() {
        return teamLeader;
    }

    public void setTeamLeader(User teamLeader) {
        this.teamLeader = teamLeader;
    }

    private List<User> members;

    @ManyToMany(fetch = LAZY, cascade = REFRESH)
    @OrderBy("firstname, lastname asc")
    @JoinTable(name = "team_has_user",
               joinColumns = { @JoinColumn(name = "team_teamId")},
               inverseJoinColumns={@JoinColumn(name="user_username")})
    public List<User> getMembers() {
        return members;
    }

    public void setMembers(List<User> members) {
        this.members = members;
    }

    private List<Section> sections;

    @ManyToMany(fetch = LAZY, cascade = REFRESH)
    @OrderBy("sectionName asc")
    @JoinTable(name = "team_has_section",
               joinColumns = { @JoinColumn(name = "team_teamId")},
               inverseJoinColumns={@JoinColumn(name="section_sectionId")})
    public List<Section> getSections() {
        return sections;
    }

    public void setSections(List<Section> sections) {
        this.sections = sections;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Team that = (Team) o;

        if (teamId != that.teamId) return false;
        if (creationDate != null ? !creationDate.equals(that.creationDate) : that.creationDate != null) return false;
        if (teamName != null ? !teamName.equals(that.teamName) : that.teamName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = teamId.intValue();
        result = 31 * result + (teamName != null ? teamName.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        return result;
    }
}
